/*
 * @文件描述: 开机后显示的主界面的module，同时管理模块的连接
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-04-18 14:54:57
 */
#include "homePage.h"
#include "main.h"
#include "MultiMeterPre.h"
#include "OscilloscopePre.h"
#include "HomePagePre.h"
#include "myButton.h"
#include "beeper.h"
static unsigned char Mode = APP_HOMEPAGE;
//模块连接状态
static uint8_t moduleState[3] = {DISCONNECTED};

static void (*ModuleCheckingCb)(uint8_t, uint8_t);
/**
 * @brief : 模块检测引脚初始化
 * @param {*}
 * @return {*}
 */
void moduleCheckInit()
{
#ifndef WIN_SIM
    keyInit(OSCILL_CHECK_GPIO_PORT, OSCILL_CHECK_GPIO_PIN, OSCILL_CHECK_GPIO_CLK, GPIO_PuPd_UP);
    keyInit(MULTIMETER_CHECK_GPIO_PORT, MULTIMETER_CHECK_GPIO_PIN, MULTIMETER_CHECK_GPIO_CLK, GPIO_PuPd_UP);
    keyInit(KEYBOARD_CHECK_GPIO_PORT, KEYBOARD_CHECK_GPIO_PIN, KEYBOARD_CHECK_GPIO_CLK, GPIO_PuPd_UP);

#endif // !WIN_SIM

}
/**
 * @brief : 主界面初始化
 * @param {uint8_t} mode 0即可
 * @return {*}
 */
void homePageInit(uint8_t mode)
{
    //moduleCheckInit();
    V_homePageInit();
}
/**
 * @brief : 设置模块插拔监听函数，在模块被插拔时，会调用监听函数
 * @param {*}
 * @return {*}
 */
void setModuleCheckingCb(void (*moduleCheckingCb)(uint8_t, uint8_t))
{
    ModuleCheckingCb = moduleCheckingCb;
}
/**
 * @brief : 检测模块是否接入
 * @param {uint8_t} module
 * @return {uint8_t} 为1时，接入，为0时，未接入
 */
static uint8_t checkModule(uint8_t module)
{
#ifndef WIN_SIM
    switch (module)
    {
    case MODULE_OSCILL:
        /* code */
        return !(GPIO_ReadInputDataBit(OSCILL_CHECK_GPIO_PORT, OSCILL_CHECK_GPIO_PIN));
        break;
    case MODULE_MULTIMETER:
        /* code */
        return !(GPIO_ReadInputDataBit(MULTIMETER_CHECK_GPIO_PORT, MULTIMETER_CHECK_GPIO_PIN));
        break;
    case MODULE_KEYBOARD:
        /* code */
        return !(GPIO_ReadInputDataBit(KEYBOARD_CHECK_GPIO_PORT, KEYBOARD_CHECK_GPIO_PIN));
        break;
    default:
        break;
    }
#else
    return 1;
#endif // !WIN_SIM

}
/**
 * @brief : 模块连接检测函数，用于检测模块是否连接，用线程或定时器运行
 * @param {*}
 * @return {*}
 */
void moduleChecking(void)
{
    uint8_t module = 1;
    for (module = 1; module < 4; module++)
    {
        if (checkModule(module) != moduleState[module - 1])
        {
            moduleState[module - 1] = checkModule(module);
            printf("module %d is %d \n", module, moduleState[module - 1]);
            beeperTrigger(30);
            if (ModuleCheckingCb != NULL)
            {
                if (checkModule(module))
                {
                    (*ModuleCheckingCb)(module, CONNECTED);
                }
                else
                {
                    (*ModuleCheckingCb)(module, DISCONNECTED);
                }
            }
        }
    }
}
/**
 * @brief : 获取模块的连接状态
 * @param {uint8_t} module
 * @return {*}
 */
uint8_t getModuleState(uint8_t module)
{
    return moduleState[module - 1];
}
/**
 * @brief : 进入所选模式，如果相应模块没有连接则无法进入
 * @param {uint8_t} mode
 * @return {*}
 */
void enterMode(uint8_t mode)
{

    switch (mode)
    {
    case APP_OSCILL:
        if (getModuleState(MODULE_OSCILL) == CONNECTED)
        {
            V_CloseHomePage();
            killMultiMeterThread();
            Oscil_Init();
            startOscillThread();
        }
        else
        {
            V_showInfo("No condi board connected!");
        }
        break;

    case APP_MULTIMETER:
        if (getModuleState(MODULE_MULTIMETER) == CONNECTED)
        {
            V_CloseHomePage();
            killOscillThread();
            multiMeterInit();
            startMultiMeterThread();
        }
        else
        {
            V_showInfo("No condi board connected!");
        }

        break;
    default:
        break;
    }
    Mode = mode;
}
/**
 * @brief : 回到主界面
 * @param {uint8_t} lastMode
 * @return {*}
 */
void backToHome(uint8_t lastMode)
{
    V_homePageInit();
}
/**
 * @brief : 取得当前模式
 * @param {*}
 * @return {*}
 */
uint8_t getCurrentApp(void)
{
    return Mode;
}
